package ecomp.uefs.micc.sharedbox.view;

import java.awt.Container;
import java.awt.FlowLayout;
import java.io.DataInputStream;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.io.*;
import javax.swing.*;

import ecomp.uefs.micc.sharedbox.model.Client;

@SuppressWarnings("serial")
public class ClientFrame extends JFrame {
	
	private Client client;
	
	public ClientFrame(){
		initComponents();
	}
	
	public static void main(String[] args){
		ClientFrame window = new ClientFrame();
		}
	
	
		// Variables Declarations	
			
	private JButton btnConnect;			
	private JTextField usernameText;
	private JTextField passwordText;			
	private JLabel username;
	private JLabel password;
		// Extras
	private JTextField txtFile;
	private JTextField txtPort;
	private JTextField txtServer;
	private JTextField txtOut;
	private JLabel lblImagem;
	private JLabel lblServer;
	private JLabel lblFile;
	private JLabel lblOut;
	private JLabel lblPort;
	private JButton btnSearch;

	private void initComponents() {
		// TODO Auto-generated method stub
		Container pane = this.getContentPane(); //captura o painel do JFrame
		pane.setLayout(new FlowLayout(FlowLayout.CENTER)); //define o layout
		// Labels
		username = new JLabel("Username: ");
		password = new JLabel("Password: ");
		//----------------------------------
		lblServer = new JLabel("Server: ");
		lblPort = new JLabel("Port: ");
		lblFile = new JLabel("File: ");
		lblOut = new JLabel("Out: ");
		
		// TextFields
		usernameText = new JTextField(8);		
		passwordText = new JTextField(8);
		
		//----------------------------------
		txtFile = new JTextField(8);
		txtOut = new JTextField(8);
		txtPort = new JTextField(8);
		txtServer = new JTextField(8);		
		
		pane.add(username);
		pane.add(usernameText);
		pane.add(password);
		pane.add(passwordText);
		//----------------------------------
		pane.add(lblServer);
		pane.add(txtServer);
		pane.add(lblPort);
		pane.add(txtPort);
		pane.add(lblFile);
		pane.add(txtFile);
		pane.add(lblOut);
		pane.add(txtOut);
//--------------------------------------------------------------------------------------------------------------------------------------		
		
		btnSearch = new JButton("Search");
		lblImagem = new JLabel();
		
		btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });
		
		
		pane.add(txtServer);
		pane.add(txtPort);
		pane.add(txtFile);
		pane.add(txtOut);
		pane.add(lblImagem);
		pane.add(btnSearch);
		
//--------------------------------------------------------------------------------------------------------------------------------------		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(450, 300);
		this.setVisible(true);
		this.setResizable(false);
		setLocationRelativeTo(null);
	}
	
	 private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {                                         

  	   try {

  	     //Cria o Socket para buscar o arquivo no servidor 

  	     Socket rec = new Socket(txtServer.getText(),Integer.parseInt(txtPort.getText()));

  	     

  	     //Enviando o nome do arquivo a ser baixado do servidor

  	     ObjectOutputStream saida = new ObjectOutputStream(rec.getOutputStream());

  	     saida.writeObject(txtFile.getText());

  	 

  	     // DataInputStream para processar os bytes recebidos

  	     DataInputStream entrada = new DataInputStream(rec.getInputStream());

  	     //FileOuputStream para salvar o arquivo recebido

  	     FileOutputStream sarq = new FileOutputStream(txtOut.getText());

  	        

  	     byte[] br = new byte[1024];

  	     int leitura = entrada.read(br);

  	     while(leitura != -1) {

  	       if(leitura != -2) {

  	         sarq.write(br,0,leitura);

  	       }

  	       leitura = entrada.read(br);

  	     }

  	     saida.close();

  	     entrada.close();

  	     sarq.close();

  	     rec.close();

  	     ImageIcon img = new ImageIcon(txtOut.getText());

  	     lblImagem.setText("");

  	     lblImagem.setIcon(img);

  	   }

  	   catch(Exception e) {

  	       JOptionPane.showMessageDialog(null,"Exceção:" + e.getMessage(),"Erro",2);

  	   }

  	}     
}
